﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Models;
using Utilities;
using Telerik.Web.Mvc;
using BLL;

namespace Grant_Warehouse.Controllers
{ 
    public class WarehouseController : Controller
    {
        public ViewResult Index()
        {
            return View(CH.GetAllData<Warehouse>());
        }
        [GridAction]
        public ActionResult AjaxWarehouseIndex()
        {
            return View(CT.GetViewModel<Warehouse,WarehouseView>("Area"));
        }

        [GridAction]
        public ActionResult AjaxStockIndex(int warehouseID)
        {
            return View( CT.GetViewModel<Stock, StockView>(s => s.WarehouseID == warehouseID, "Category", "quality"));
        }

        public ActionResult Charts(int id)
        {
            ViewBag.WarehouseID = id;
            return View(id);
        }

        public ActionResult AjaxStockPieChart(int id) 
        {
            var source = CT.GetViewModel<Stock, StockView>(s => s.WarehouseID == id);
            source.Data = ViewManagement.CalculatePercentage(source.Data as List<StockView>);
            return Json(source.Data); 
        }

        public ActionResult AjaxStockBarChart(int id)
        {
            var source = CT.GetViewModel<Stock, StockView>(s => s.WarehouseID == id);
            source.Data = ViewManagement.CalculateCategoryTotal(source.Data as List<StockView>);
            return Json(source.Data);
        }

        public ViewResult Details(int id)
        {
            return View(CH.GetDataById<Warehouse>(id));
        }

        public ActionResult Create()
        {
            return View();
        } 

        [HttpPost]
        public ActionResult Create(Warehouse warehouse)
        {
            if (ModelState.IsValid)
            {
                CH.Create<Warehouse>(warehouse);
                return RedirectToAction("Index");  
            }

            return View(warehouse);
        }
 
        public ActionResult Edit(int id)
        {
            return View(CH.GetDataById<Warehouse>(id));
        }

        public JsonResult GetDataByAreaIdJson(int areaid)
        {
            var data = CH.GetAllData<Warehouse>(w=>w.AreaID == areaid);
            List<object> ws = new List<object>();
            data.ForEach(d => ws.Add(new { warehousename= d.WarehouseName,id=d.ID}));
            return Json(ws,JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Edit(Warehouse warehouse)
        {
            if (ModelState.IsValid)
            {
                CH.Edit<Warehouse>(warehouse);
                return RedirectToAction("Index");
            }
            return View(warehouse);
        }
 
        public ActionResult Delete(int id)
        {
            return View(CH.GetDataById<Warehouse>(id));
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                CH.Delete<Warehouse>(id);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, "此数据被其他数据引用，为保证数据完整性，请先移除对此数据的引用，再进行删除");
                Log.LogError(ex, this);
                return View(CH.GetDataById<Warehouse>(id));
            }
            return RedirectToAction("Index");
        }
    }
}